#!/bin/sh
# Create a link to this file at .git/hooks/pre-commit to
# force PEP8 and Black style validation prior to committing

exec 1>&2

EXIT=0
RED='\033[0;31m'
NOCOLOR='\033[0m'

if [ -d ./venv/ ]; then
    VENV="$PWD/venv"
    if [ -e $VENV/bin/python ]; then
        PATH=$VENV/bin:$PATH
    elif [ -e $VENV/Scripts/python.exe ]; then
        PATH=$VENV/Scripts:$PATH
    fi
fi

echo "Validating PEP8 compliance..."
invoke flake8
if [ $? != 0 ]; then
	EXIT=1
fi

echo "Check that all files conform to Black..."
invoke black
if [[ $? != 0 ]]; then
    EXIT=1
fi

echo "Checking for missing migrations..."
invoke check-migrations
if [ $? != 0 ]; then
	EXIT=1
fi

HADOLINT_PATH=$(which hadolint)
if [ -x $HADOLINT_PATH ]; then
	echo "Checking Dockerfile..."
	hadolint docker/Dockerfile
	if [ $? != 0 ]; then
		EXIT=1
	fi
fi

if [ $EXIT != 0 ]; then
	printf "${RED}COMMIT FAILED${NOCOLOR}\n"
fi

exit $EXIT
